package com.association.main.controller;


import com.association.main.common.R;
import com.association.main.domain.*;
import com.association.main.dto.ApplyJoinDto;
import com.association.main.service.ApplyJoinService;
import com.association.main.service.ClubsService;
import com.association.main.service.JoinInfoService;
import com.association.main.service.MembersService;
import com.association.main.utils.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/applyJoin")
public class ApplyJoinController {
    @Autowired
    private ApplyJoinService applyJoinService;
    @Autowired
    private ClubsService clubsService;

    @Autowired
    private JoinInfoService joinInfoService;

    @Autowired
    private MembersService membersService;

    @GetMapping
    public R<Page> applyJoinList(int page, int pageSize, ApplyJoin join, HttpServletRequest request){
        Page pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<ApplyJoin> applyJoinLambdaQueryWrapper=new LambdaQueryWrapper<>();
        applyJoinLambdaQueryWrapper.like(join.getClubId()!=null,ApplyJoin::getClubId,join.getClubId());
        applyJoinLambdaQueryWrapper.orderByDesc(ApplyJoin::getCreateTime);
        List<ApplyJoin> applyJoinList = applyJoinService.list(applyJoinLambdaQueryWrapper);
        applyJoinService.page(pageInfo,applyJoinLambdaQueryWrapper);
        //获取页面的数据
        List<ApplyJoinDto> applyJoinDtos = applyJoinList.stream().map((item)->{
            ApplyJoinDto applyJoinDto=new ApplyJoinDto();
            BeanUtils.copyProperties(item,applyJoinDto);
            Clubs clubsServiceById = clubsService.getById(item.getClubId());
            Members membersServiceById = membersService.getById(item.getMemberId());
            applyJoinDto.setClubs(clubsServiceById);
            applyJoinDto.setMembers(membersServiceById);
            return applyJoinDto;
        }).collect(Collectors.toList());

        // 处理权限问题
        String token = request.getHeader("token");
        String userType = JwtUtil.getUserType(token);
        if ("member".equals(userType)) {
            //如果是member用户，则只显示clubs_id有包含在的
            Integer userId = JwtUtil.getUserId(token);
            LambdaQueryWrapper<Clubs> clubsWrapper = new LambdaQueryWrapper<>();
            clubsWrapper.eq(Clubs::getCreatedBy, userId);
            List<Clubs> clubsList = clubsService.list(clubsWrapper);
            applyJoinDtos = applyJoinDtos.stream().filter(m -> clubsList.stream().anyMatch(c -> m.getClubs().equals(c))).collect(Collectors.toList());
        }
        pageInfo.setRecords(applyJoinDtos);
        return R.success(pageInfo);
    }
    @Transactional
    @PostMapping("/status")
    public R<String> applyStatus( int id, int status){
        LambdaUpdateWrapper<ApplyJoin> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ApplyJoin::getId, id).set(ApplyJoin::getStatus, status);
        boolean success = applyJoinService.update(lambdaUpdateWrapper);
        if (success) {
            ApplyJoin byId = applyJoinService.getById(id);
            Integer clubId = byId.getClubId();
            Integer memberId = byId.getMemberId();
            JoinInfo joinInfo=new JoinInfo();
            joinInfo.setClubId(clubId);
            joinInfo.setMemberId(memberId);
            joinInfoService.save(joinInfo);
            return R.success("审核成功");
        } else {
            return R.error("审核失败");
        }
    }

    @PostMapping("/Rejectstatus")
    public R<String> applyRejectStatus( int id,String msg){
        LambdaUpdateWrapper<ApplyJoin> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ApplyJoin::getId,id);
        lambdaUpdateWrapper.set(ApplyJoin::getStatus,-1);
        lambdaUpdateWrapper.set(ApplyJoin::getMsg,msg);
        boolean success = applyJoinService.update(lambdaUpdateWrapper);
        if (success) {
            return R.success("审核成功");
        } else {
            return R.error("审核失败");
        }
    }

    @DeleteMapping
    public R<String> delete(@RequestBody ApplyJoin applyJoin){
        applyJoinService.removeById(applyJoin.getId());
        return R.success(20000,"删除成功");
    }

}
